1
|
|
|
import { |
2
|
|
|
BadRequestException, |
3
|
|
|
Body, |
4
|
|
|
Controller, |
5
|
|
|
Get, |
6
|
|
|
Inject, |
7
|
|
|
Post, |
8
|
|
|
Render, |
9
|
|
|
Res, |
10
|
|
|
UseGuards |
11
|
|
|
} from '@nestjs/common'; |
12
|
|
|
import { LoggedUser } from '../Decorator/LoggedUser'; |
13
|
|
|
import { User } from 'src/Domain/HumanResource/User/User.entity'; |
14
|
|
|
import { UserView } from 'src/Application/HumanResource/User/View/UserView'; |
15
|
|
|
import { IsAuthenticatedGuard } from '../Security/IsAuthenticatedGuard'; |
16
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
17
|
|
|
import { ProfileDTO } from '../DTO/ProfileDTO'; |
18
|
|
|
import { UpdateProfileCommand } from 'src/Application/HumanResource/User/Command/UpdateProfileCommand'; |
19
|
|
|
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; |
20
|
|
|
import { Response } from 'express'; |
21
|
|
|
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; |
22
|
|
|
|
23
|
|
|
@Controller('app/profile/edit') |
24
|
|
|
@UseGuards(IsAuthenticatedGuard) |
25
|
|
|
export class EditProfileController { |
26
|
|
|
constructor( |
27
|
|
|
@Inject('ICommandBus') |
28
|
|
|
private readonly commandBus: ICommandBus, |
29
|
|
|
private readonly resolver: RouteNameResolver |
30
|
|
|
) {} |
31
|
|
|
|
32
|
|
|
@Get() |
33
|
|
|
@WithName('profile_edit') |
34
|
|
|
@Render('pages/profile/edit.njk') |
35
|
|
|
public async get(@LoggedUser() user: User) { |
36
|
|
|
const me = new UserView( |
37
|
|
|
user.getId(), |
38
|
|
|
user.getFirstName(), |
39
|
|
|
user.getLastName(), |
40
|
|
|
user.getEmail(), |
41
|
|
|
user.getRole(), |
42
|
|
|
false |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
return { user: me }; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
@Post() |
49
|
|
|
public async post( |
50
|
|
|
@Body() dto: ProfileDTO, |
51
|
|
|
@LoggedUser() user: User, |
52
|
|
|
@Res() res: Response |
53
|
|
|
) { |
54
|
|
|
try { |
55
|
|
|
const { firstName, lastName, email, password } = dto; |
56
|
|
|
await this.commandBus.execute( |
57
|
|
|
new UpdateProfileCommand(user, firstName, lastName, email, password) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
res.redirect(303, this.resolver.resolve('home')); |
61
|
|
|
} catch (e) { |
62
|
|
|
throw new BadRequestException(e.message); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|